// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package http

import (
	"html/template"
	"path"

	"github.com/ethereum/go-ethereum/swarm/api"
)

type htmlListData struct {
	URI  *api.URI
	List *api.ManifestList
}

var htmlListTemplate = template.Must(template.New("html-list").Funcs(template.FuncMap{"basename": path.Base}).Parse(`
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="shortcut icon" type="image/x-icon" href=""/>
	<title>Swarm index of {{ .URI }}</title>
</head>

<body>
  <h1>Swarm index of {{ .URI }}</h1>
  <hr>
  <table>
    <thead>
      <tr>
	<th>Path</th>
	<th>Type</th>
	<th>Size</th>
      </tr>
    </thead>

    <tbody>
      {{ range .List.CommonPrefixes }}
	<tr>
	  <td><a href="{{ basename . }}/">{{ basename . }}/</a></td>
	  <td>DIR</td>
	  <td>-</td>
	</tr>
      {{ end }}

      {{ range .List.Entries }}
	<tr>
	  <td><a href="{{ basename .Path }}">{{ basename .Path }}</a></td>
	  <td>{{ .ContentType }}</td>
	  <td>{{ .Size }}</td>
	</tr>
      {{ end }}
  </table>
  <hr>
</body>
`[1:]))

var landingPageTemplate = template.Must(template.New("landingPage").Parse(`
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" ww="chrome=1">
    <meta name="description" content="Ethereum/Swarm Landing page">
		<style>

      body, div, header, footer {
        margin: 0;
        padding: 0;
      }

      body {
        overflow: hidden;
      }

      .container {
        min-width: 100%;
        min-height: 100%;
        max-height: 100%;
      }

      header {
        display: flex;
        align-items: center;
        background-color: #ffa500;
        /* height: 20vh; */
        padding: 5px;
      }

      .header-left, .header-right {
        width: 20%;
      }

      .header-left {
        padding-left: 40px;
        float: left;
      }

      .header-right {
        padding-right: 40px;
        float: right;
      }

      .page-title {
        /* margin-top: 4.5vh; */
        text-align: center;
        float:      left;
        width:      60%;
        color:      white;
      }

      content-body {
        display: block;
        margin: 0 auto;
				text-align: center;
        /* width: 50%; */
        min-height: 60vh;
        max-height: 60vh;
        padding: 50px 20px;
        opacity: 0.6;
        background-color: #A9F5BF;
      }

      table {
        font-size: 1.2em;
        margin: 0 auto;
      }

      tr {
        height: 60px;
      }

      td {
        text-align: center;
      }

      .key {
        color: #111;
        font-weight: bold;
        width: 200px;
      }

      .value {
        color: red;
        font-weight: bold
      }

      footer {
        height: 20vh;
        background-color: #ffa500;
        font-size: 1em;
        text-align: center;
        padding: 20px;
      }

    </style>
    <title>Swarm :: Welcome to Swarm</title>
  </head>
  <body>


      <header>
        <div class="header-left">
          <img style="height:18vh;margin-left:40px" src=""/>
        </div>
        <div class="page-title">
          <h1>Welcome to Swarm</h1>
        </div>
      </header>

			<script type="text/javascript">
			function goToPage() {
					var page = document.getElementById('page').value;
					if (page == "") {
						var page = "theswarm.eth"
					}
					var address = "/bzz:/" + page;
					location.href = address;
					console.log(address)
			}
			</script>
				<content-body>

	            <h1>Enter the hash or ENS of a Swarm-hosted file below:</h1>
								<input type="text" id="page" size="64"/>
								<input type="submit" value="submit" onclick="goToPage();" />

				</content-body>
      <footer>
        <p>
          Swarm: Serverless Hosting Incentivised Peer-To-Peer Storage And Content Distribution<br/>
          <a href="/bzz:/theswarm.eth">Swarm</a>
        </p>
      </footer>

  </body>
</html>
`[1:]))
